package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.CatalogItemDTO;
import static memberrewards.helpers.Constants.BLANK;

public class PlaceOrder extends ServletPojoAdapter {
	private static final long serialVersionUID = 1810714480058936728L;

	protected Map<String,String> getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan =params.get("pan")!=null? params.get("pan")[0]:null;
		final String itemId =params.get("itemid")!=null? params.get("itemid")[0]:null;
		
		//Default response
		final Map<String,String> ret = new HashMap<String, String>();
		ret.put("status", "Card Not Found");		
		ret.put("balance", "0");

		//Not a Valid Item
		if (itemId == null || BLANK.equals(itemId)) {
			ret.put("status", "Item information is not available");
		} else {
			//Valid Input
			//Check for Valid account number
			final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);
			//Check for Valid catalogItem
			final CatalogItemDTO catalogItem = RewardsDelegate.getCatalogItem(itemId);
			if (accountDetail!=null){
				final int pointsBalance = new Integer(accountDetail.getPointBalance());
				//Item not found -Inform user
				if(catalogItem == null){
					ret.put("status", "Item could not found");
				}
				else{
					//Item Found -Check member is eligible for this Tier
					if(catalogItem.getTierList().contains(accountDetail.getTier())){
						//Check member has enough point balance
						if(pointsBalance >= catalogItem.getPointCost()){
							final int newBalance = pointsBalance - catalogItem.getPointCost();
							accountDetail.setPointBalance(String.valueOf(newBalance));
							final boolean status=RewardsDelegate.updateAccountDetail(accountDetail);
							ret.put("balance", Integer.toString(newBalance));
							ret.put("status",status? "Success" : "Failed");
						}
						else{
							ret.put("status", "Insufficient Points Balance");
						}
					}
					else{
						ret.put("status", "Not Eligible to place the order");
					}
				}
//				final ArrayList<CatalogItem> items = CatalogServiceDAO
//						.getEligibleCatalogList(accountDetail.getTier());
//
//				if (items == null || items.isEmpty()) {
//					ret.put("status", "Not Eligible to place the order");
//				}				
//				if (items != null && !items.isEmpty()) {
//					ret.put("status", "Item could not be found");
//					for (final CatalogItem item : items) {
//						if(item.getItemid().equals(itemId)){
//							if(pointsBalance >= item.getPointCost()){
//								try {
//									int newBalance = pointsBalance - item.getPointCost();
//									accountDetail.setPointBalance(String.valueOf(newBalance));
//									boolean status=RewardsDelegate.updateAccountDetail(accountDetail);
//									ret.put("balance", Integer.toString(newBalance));
//									ret.put("status",status? "Success" : "Failed");
//								} catch (Exception e) {
//									ret.put("balance", accountDetail.getPointBalance());
//									ret.put("status", "Failed");
//								}
//
//							}else{
//								ret.put("status", "Insufficient Points Balance");
//							}
//							break;
//						}
//
//					}
//				}else{
//					ret.put("status", "Not Eligible to place the order");
//					
//				}
			}
		}
		return ret;
	}
}
